package p.ithorns.integration.transform.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import p.ithorns.integration.transform.entity.*;
import p.ithorns.integration.transform.enums.OrderType;
import p.ithorns.integration.transform.mapper.UserMapper;
import p.ithorns.tools.translator.annotation.Translatable;
import p.ithorns.tools.translator.sdk.provider.DictModelProvider;
import p.ithorns.tools.translator.sdk.provider.DictStandardProvider;

import java.util.*;
import java.util.stream.Collectors;

/**
 * UserService
 *
 * @author Ht.L
 * @version 1.0.0
 * @date 2023/4/5 19:06
 */
@Slf4j
@Service
public class UserService extends ServiceImpl<UserMapper, User>
        implements DictStandardProvider<Integer, String>, DictModelProvider<Integer, User> {

    @Autowired
    UserMapper mapper;

    @Override
    public String getDict(Integer key) {
        User user = mapper.selectById(key);
        return user.getName();
    }

    @Override
    public Map<Integer, String> getDictMap(Set<Integer> keys) {
        List<User> users = mapper.selectByIds(keys);
        return users.stream().collect(Collectors.toMap((User::getId), User::getName));
    }

    @Override
    public User getModel(Integer key) {
        return  mapper.selectById(key);
    }

    @Override
    public Map<Integer, User> getModelMap(Set<Integer> keys) {
        List<User> users = mapper.selectByIds(keys);
        return users.stream().collect(Collectors.toMap((User::getId), v-> v, (v1, v2) -> v1));
    }


    @Translatable
    public Account getAccount() {
        Random random = new Random();
        Account account = new Account();
        account.setId(1000 + 1);
        account.setUserId(1);
        account.setStatus(String.valueOf(random.nextInt(2)));
        return account;
    }

    @Translatable
    public List<User> listUsers() {
        List<User> list = list();
        int i = 1;
        Random random = new Random();
        for (User user : list) {
            Integer userId = user.getId();
            List<Order> orders = buildOrder(user.getName(), userId);
            user.setOrders(orders);
            Account account = new Account();
            account.setId(userId * 1000 + i );
            account.setUserId(userId);
            account.setStatus(String.valueOf(random.nextInt(2)));
            user.setAccount(account);
            i++;
        }
        return list;
    }

    @Translatable
    public User getUser() {
        User user = super.getById(1);
        Integer userId = user.getId();
        Random random = new Random();
        Account account = new Account();
        account.setId(userId * 1000 + 1);
        account.setStatus(String.valueOf(random.nextInt(2)));
        user.setAccount(account);

        List<Order> orders = buildOrder(user.getName(), userId);
        user.setOrders(orders);
        log.info("原始结果：{}", user);
        return user;
    }

    private List<Order> buildOrder(String userName, Integer userId) {
        List<Order> orders = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            int index = i + 1;
            OrderExt order = new OrderExt();
            order.setUserId(userId);
            order.setIndex(100 + index);
            order.setId(index);
            order.setType(OrderType.random().getCode());
            order.setStatus(String.valueOf(random.nextInt(2)));

            OrderDetail detail = new OrderDetail();
            detail.setOrderId(index);
            detail.setEnabled(String.valueOf(random.nextInt(2)));
            order.setDetail(detail);
            orders.add(order);
        }
        return orders;
    }

    @Translatable
    public User get(Integer id) {
        User user = new User();
        user.setId(id);
        user.setName("UserXxx");
        List<Order> orders = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            Order order = new Order();
//            order.setId(i + 1);
//            order.setType(OrderType.random().getCode());
//            order.setTime(LocalDateTime.now());
            orders.add(order);
        }
        user.setOrders(orders);
        return user;
    }



}